<template>
  <div class="layout">
    <div class="grow">
      <div>
        <div class="staff scrolll" @scroll="scroll">
          <div class="top_box">
            <div class="top">
              <div :class="['top_btn', materialId == 0 ? 'active' : '']" @click="filterMaterial(0)">违规员工/客户</div>
              <div :class="['top_btn', materialId == 1 ? 'active' : '']" @click="filterMaterial(1)">违规群聊</div>
            </div>
            <!-- end单聊群聊 -->
          </div>
          <!-- 单聊群聊 -->
          <div class="chat" v-if="materialId == 0">
            <ul>
              <li class="onbu" v-for="item in customerStaff" :key="item.id" @click="chatDetail(item)" :class="selectId == item.id ? 'hover-detail-color' : ''">
                <el-image :src="item.avatar" class="left-img">
                  <template #error>
                    <el-image src="/static/file/pic.png"></el-image>
                  </template>
                </el-image>
                <div class="center">
                  <span class="text" style="">{{ item.name }}</span>
                  <span class="text" style="font-size: 14px; color: #33302d">{{ item.type == 2 ? '(客户)' : '(员工)' }}</span>
                </div>
              </li>
            </ul>
          </div>
          <div class="chat" v-else>
            <ul>
              <li class="onbug" v-for="item in violationGroup" :key="item.id" @click="chatDetail(item)" :class="selectId == item.id ? 'hover-detail-color' : ''">
                <el-image :src="item.avatar" class="left-img">
                  <template #error>
                    <el-image src="/static/file/pic.png"></el-image>
                  </template>
                </el-image>
                <span class="textg">{{ item.name }} ({{ item.member_nums }}人)</span>
              </li>
            </ul>
          </div>
        </div>
      </div>
        <!-- 违规记录模块 -->
        <violationRecord ref="violationRef" :chatDetailList="chatDetailList" :violationChat="violationChat" :wx_userid="wx_userid" :roomid="roomid" :ltType="materialId"></violationRecord>
        <!-- end聊天记录模块 -->
    </div>
  </div>
</template>
<script setup lang="ts">
  import { reactive, ref, provide, onMounted } from 'vue'
  import { violationCustomerStaff, getViolationGroup, getViolationChat, getViolationGroupChat } from '@/api/employe/riskmonitoring/violationMonitoring'
  import violationRecord from './components/violationRecord.vue'

  const materialId = ref(0) // 聊天选择
  const customerStaff: any = ref([])
  const violationGroup: any = ref([])
  const violationRef = ref(null)
  const selectId = ref(0)
  const chatDetailList = ref([])
  const pagination = reactive({
    currentPage: 1,
    pageSize: 15,
    total: ''
  })
  const getPagination = (data: any) => {
    pagination.currentPage = data.page
    pagination.pageSize = data.page_size
  }

  // 下拉获取违规员工和违规群聊参数
  let violation = reactive({
    status: 0,
    page: 1,
    page_size: 10
  })
  // 聊天筛选
  const filterMaterial = (v: any) => {
    materialId.value = v
    violationRef.value.violationChat = []
    violationRef.value.times = ''
    if (materialId.value == 1) {
      getViolationGroup(violation).then(({ data }: any) => {
        violationGroup.value = data
        if(data.length > 0){
          chatDetail(data[0])
        }
      })
    }else{
      if(customerStaff.value.length > 0){
        chatDetail(customerStaff.value[0])
      }
    }
  }
  // 获取员工列表
  violationCustomerStaff(violation).then(({ data }: any) => {
    customerStaff.value = data
    if(data.length > 0){
      chatDetail(data[0])
    }
  })
  let wx_userid = ref('')
  let roomid = ref('')
  // 点击用户
  const chatDetail = (item: any) => {
    violationRef.value.start = true
    chatDetailList.value = item
    if (materialId.value == 0) {
      wx_userid.value = item.from
    } else {
      roomid.value = item.roomid
    }
    selectId.value = item.id
    violationRef.value.paged = 1
    violationRef.value.pageg = 1

    console.log(violationRef.value.start, 'start')
  }
  let starts = ref(true)
  const paged = ref(1)
  const pageg = ref(1)
  const scroll = async () => {
    const el = document.querySelector('.scrolll')
    const clientHeight = el?.clientHeight
    const scrollHeight = el?.scrollHeight
    const scrollTop = Math.ceil(el?.scrollTop)
    console.log(scrollHeight, clientHeight)
    console.log(scrollTop)
    console.dir(el)
    if (scrollTop >= scrollHeight - clientHeight) {
      if (!starts.value) return
      if (materialId.value == 0) {
        let params = {
          status: 0,
          page: ++paged.value, //分页页码
          page_size: 10 //每页显示数
        }
        const { data } = await violationCustomerStaff(params)
        console.log('data', data)
        if (data.length === 0) {
          starts.value = false
          return console.log('没有数据了')
        }
        customerStaff.value.push(...data)
      } else {
        let paramss = {
          status: 0,
          page: ++pageg.value, //分页页码
          page_size: 10 //每页显示数
        }
        const { data } = await getViolationGroup(paramss)
        console.log('data', data)
        if (data.length === 0) {
          starts.value = false
          return console.log('没有数据了')
        }
        violationGroup.value.push(...data)
      }
      starts.value = true
    }
  }
</script>

<style lang="scss" scoped>
  .layout {
    // padding: 16px;
    .grow {
      display: flex;
    }
    .staff {
      min-width: 254px;
      height: 800px;
      border: 1px solid #e9e9e9;
      padding: 16px 0;
      margin-right: 16px;
      background-color: #fff;
      box-sizing: border-box;
      .top_box {
        padding: 0 16px;
      }
      .chat {
        margin-top: 16px;
        .onbug {
          display: flex;
          padding: 16px 16px 0;
          height: 52px;
          line-height: 40px;
          cursor: pointer;
          .textg {
            font-size: 14px;
            color: #33302d;
          }
        }
        .onbu {
          display: flex;
          padding: 16px 16px 0;
          height: 52px;
          cursor: pointer;
          .center {
            width: 100%;
            height: 52px;
            line-height: 38px;
            .text {
              font-size: 14px;
              color: #33302d;
            }
          }
          :deep .el-image {
            flex: 0 0 36px;
            width: 36px;
            height: 36px;
            vertical-align: middle;
            .el-image__inner {
              border-radius: 4px;
            }
          }
        }

        .left-img {
          width: 36px;
          height: 36px;
          line-height: 36px;
          margin-right: 10px;
          border-radius: 4px;
        }
        .hover-detail-color {
          background-color: #fffaf9;
        }
      }
    }
    .scrolll {
      height: 81.5vh;
      overflow: scroll;
      // overflow-x: hidden;
      // &::-webkit-scrollbar {
      //   display: none;
      // }
    }
    .extend_select {
      margin-top: 16px;
    }
    .top {
      display: flex;
      width: 100%;
      &_btn {
        flex: 1;
        padding: 5px 0;
        background: #ffffff;
        border: 1px solid #ff6b00;
        font-size: 12px;
        color: #ff6b00;
        line-height: 16px;
        cursor: pointer;
        margin-top: 10px;
        text-align: center;
      }
      .active {
        background: #ff6b00;
        color: #ffff;
      }
    }
  }
</style>
